package com.communal.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

/**
 * Application utility class <br>
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Component
public class ApplicationUtils {
	
	private static final String DATE_FORMAT_DMY = "dd-MM-yyyy";
	private static final String DATE_FORMAT_ISO = "yyyy-MM-dd'T'HH:mm'Z'";
	
	private static AppCalendarISO appCalendarISO;
	
	@Autowired
	private MessageSource messageSource;
	
	/**
	 * Parses date from iso format <br>
	 * @param p_isoDate - date in iso format; {@link String} <br>
	 * @return calendar parsed from iso date; {@link Calendar}
	 */
	private static Calendar getCalendar(String p_isoDate){
		if(appCalendarISO == null){
			appCalendarISO = new AppCalendarISO();
		}
		return appCalendarISO.getCalendar(p_isoDate);
	}
	
	/**
	 * Converts date from long to string in DMY format <br>
	 * @param p_date - date in milliseconds; {@link Long} <br>
	 * @return {@link String}
	 */
	public static String getDateDMYFromLong(Long p_date){
		return getDateFromLong(p_date, DATE_FORMAT_DMY);
	}
	
	/**
	 * Converts date from long to string in ISO format <br>
	 * @param p_date - date in milliseconds; {@link Long} <br>
	 * @return {@link String}
	 */
	public static String getDateISOFromLong(Long p_date){
		return getDateFromLong(p_date, DATE_FORMAT_ISO);
	}
	
	/**
	 * Convert date from long to string in given format <br>
	 * @param p_date - date in milliseconds; {@link Long} <br>
	 * @param p_format - date format; {@link String} <br>
	 * @return date in string format; {@link String}
	 */
	private static String getDateFromLong(Long p_date, String p_format){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(p_date);
		DateFormat dateFormat = new SimpleDateFormat(p_format);
		return dateFormat.format(calendar.getTime());
	} 
	
	/**
	 * Parse date in ISO format to milliseconds
	 * @param isodate - date in ISO format <br>
	 * @return {@link Long}
	 */
	public static Long parseISODateToLong(String isodate) {
	    Calendar calendar = getCalendar(isodate);
	    return calendar.getTimeInMillis();
	}
	
	/**
	 * Returns date of first day of current week <br>
	 * @return {@link Long}
	 */
	public Long getStartOfCurrentWeek(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTimeInMillis();
	}
	  
	/**
	 * Returns date of first day of current month <br>
	 * @return {@link Long}
	 */
	public Long getStartOfCurrentMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * Returns month and year label for displaying on chart axis <br>
	 * @param p_month - number of month (0-11) {@link Short} <br> 
	 * @param p_year - number of year (for examble 2011)
	 * @return string and month label {@link String}
	 */
	public String getMonthYearLabel(Short p_month, Short p_year){
		StringBuffer label = new StringBuffer();
		if(p_month != null && p_year != null){
			label.append( messageSource.getMessage( AppConstants.MONTHS_NAMES.get( p_month ), null, null ) );
			label.append( AppConstants.SYMBOL_DEFIS );
			label.append( p_year );
		}
		return label.toString();
	}
}
